﻿Imports System.IO
Public Class AutoUpdate_Download

    Function GetLatestVersion()
        Dim line As String
        If Not File.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\Auto\version.txt") Then
            DownloadClass.GetFile(My.Resources.Update_URL, System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\Auto\")
        End If
        Using reader As StreamReader = New StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\Auto\version.txt")
            line = reader.ReadLine
        End Using
        Dim versionVar As String() = line.Split(",")
        Return versionVar(0)
    End Function

    Private Sub AutoUpdate_Download_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Icon = My.Resources.icon
        textlabel.Text = "You are running Build " & My.Resources.Build_Version & " whilst the newest currently available verison is Build " & GetLatestVersion(0)
        'Was only outputting first character
        Dim newVersion As String = GetLatestVersion()
        textlabel.Text = "You are running Build " & My.Resources.Build_Version & " whilst the newest currently available verison is Build " & newVersion 'GetLatestVersion(0)

        Dim configfile As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\User.xml"
        If ReadXML.Read(configfile, "UserConfig", "AutoUpdate") = "1" Then
            Updates_enable_checkbox.Checked = True
        Else
            Updates_enable_checkbox.Checked = False
        End If

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updates_enable_checkbox.Click
        Dim configfile As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Config\User.xml"
        If ReadXML.Read(configfile, "UserConfig", "AutoUpdate") = "1" Then
            ReadXML.Modify(configfile, "UserConfig", "AutoUpdate", "0")
        Else
            ReadXML.Modify(configfile, "UserConfig", "AutoUpdate", "1")
        End If
    End Sub

    Private Sub Download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Download.Click
        Dim line As String
        Using reader As StreamReader = New StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\Auto\version.txt")
            line = reader.ReadLine
        End Using
        Dim versionVar As String() = line.Split(",")
        DownloadClass.GetFile(versionVar(1), System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\")

        Try
            Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\" & versionVar(2))
        Catch ex As Exception
            MsgBox("Error: Unable to execute file. Please visit support for more information.")
        End Try
    End Sub

    Private Sub CloseForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseForm.Click
        Me.Close()
        Me.Dispose()
    End Sub
End Class
